package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.pojo.dto.PermissionQueryDto;
import com.community.pojo.entity.Permission;
import com.community.pojo.entity.Result;
import com.community.pojo.entity.Permission;
import com.community.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/permission")
@Tag(name = "Permission相关的接口")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/list")//代表访问路径
    @Operation(summary = "查询Permission列表")
    @Tool(description = "查询Permission列表")
    public Result list(@ToolParam @ModelAttribute PermissionQueryDto permissionQueryDto){

        //1.构建分页条件
        Page<Permission> page=new Page<>(permissionQueryDto.getPage(),permissionQueryDto.getLimit());

        //2.构建查询条件
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(permissionQueryDto.getName())){
            queryWrapper.like("name",permissionQueryDto.getName());
        }

        //3.查询
        Page<Permission> permissionPage = permissionService.page(page, queryWrapper);

        return Result.buildSuccess(permissionPage.getRecords(),permissionPage.getTotal());
    }


    @GetMapping("/findAll")
    @Operation(summary = "查询所有Permission")
    @Tool(description = "查询所有Permission")
    public Result findAll() throws Exception {

        //1.查询所有Permission
        List<Permission> permissionList = permissionService.list();

        //2.封装返回结果
        return Result.buildSuccess(permissionList);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询Permission信息")
    @Tool(description = "根据id查询Permission信息")
    public Result find(@ToolParam Long id){

        if(id==null){
            return Result.buildFail(20001,"id不能为空");
        }

        //1.查询数据
        Permission permission = permissionService.getById(id);

        //2.返回结果
        return Result.buildSuccess(permission);
    }
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询Permission信息")
    @Tool(description = "根据id查询Permission信息")
    public Result findByid(@ToolParam @PathVariable Long id){

        if(id==null){
            return Result.buildFail(20001,"id不能为空");
        }

        //1.查询数据
        Permission permission = permissionService.getById(id);

        //2.返回结果
        return Result.buildSuccess(permission);
    }


    @PostMapping("/add")
    @Operation(summary = "添加Permission信息")
    @Tool(description = "添加Permission信息")
    public Result add(@ToolParam @RequestBody Permission permission){//代表前端给后端发送Json数据

        //1.调用service进行添加
        permission.setCreated(LocalDateTime.now());
        permissionService.save(permission);

        //2.返回结果
        return Result.buildSuccess();
    }


    @PostMapping("/update")
    @Operation(summary = "修改Permission信息")
    @Tool(description = "修改Permission信息")
    public Result update(@ToolParam @RequestBody Permission permission){//代表前端给后端发送Json数据
        //1.调用service进行修改
        permissionService.updateById(permission);

        //2.返回结果
        return Result.buildSuccess();
    }

    @GetMapping("/delete")
    @Operation(summary = "根据id删除Permission信息")
    @Tool(description = "修改Permission信息")
    public Result delete(@ToolParam Long id){

        if(id==null){
            return Result.buildFail(20001,"id不能为空");
        }

        //1.调用service进行删除
        permissionService.removeById(id);

        //2.返回结果
        return Result.buildSuccess();
    }


}